home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / PixMap2PixPat2ppat / PixMap2PixPat2ppat.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  9.7 KB  |  355 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        PixMap2PixPat2ppat.c
  3.  
  4.     Contains:    This snippet shows how to convert a 'icl8' image to a    
  5.                 PixMap image and then to a PixPat and then finally to        
  6.                 a 'ppat' resource.    In this example, the 'ppat'                
  7.                 resource is saved-off into a resource file.    
  8.  
  9.     Written by: EL    
  10.  
  11.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  12.                 ("Apple") in consideration of your agreement to the following terms, and your
  13.                 use, installation, modification or redistribution of this Apple software
  14.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  15.                 please do not use, install, modify or redistribute this Apple software.
  16.  
  17.                 In consideration of your agreement to abide by the following terms, and subject
  18.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  19.                 copyrights in this original Apple software (the "Apple Software"), to use,
  20.                 reproduce, modify and redistribute the Apple Software, with or without
  21.                 modifications, in source and/or binary forms; provided that if you redistribute
  22.                 the Apple Software in its entirety and without modifications, you must retain
  23.                 this notice and the following text and disclaimers in all such redistributions of
  24.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  25.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  26.                 Apple Software without specific prior written permission from Apple.  Except as
  27.                 expressly stated in this notice, no other rights or licenses, express or implied,
  28.                 are granted by Apple herein, including but not limited to any patent rights that
  29.                 may be infringed by your derivative works or by other works in which the Apple
  30.                 Software may be incorporated.
  31.  
  32.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  33.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  34.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  36.                 COMBINATION WITH YOUR PRODUCTS.
  37.  
  38.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  39.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  40.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  42.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  43.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  44.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45.  
  46.     Change History (most recent first):
  47.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  48.                                             for demonstration purposes.
  49.                 5/9/2000    DH                Updated to use GetPixRowBytes() and not leak PixMap.
  50.                 7/13/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  51.                 
  52.  
  53. */
  54. #include "CarbonPrefix.h"
  55. #include <Types.h>
  56. #include <Memory.h>
  57. #include <Windows.h>
  58. #include <Fonts.h>
  59. #include <Dialogs.h>
  60. #include <TextEdit.h>
  61. #include <Events.h>
  62. #include <Resources.h>
  63. #include <Script.h>
  64.  
  65.  
  66.  
  67. /* Constant Declarations */
  68.  
  69. #define    WWIDTH        320
  70. #define    WHEIGHT        160
  71.  
  72. //#define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  73. //#define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  74.  
  75. /* Global Variable Definitions */
  76.  
  77. WindowPtr    gWindow;
  78.  
  79. void initMac();
  80. void createWindow();
  81. void doEventLoop();
  82. void drawWindow();
  83.  
  84. void createPixMap();
  85.  
  86. void PixMap2PixPat();
  87. void PixPat2ppat();
  88. void ppat2file(Handle, Str255);
  89.  
  90. void main()
  91. {
  92.     PixMapHandle    pixmap;
  93.     PixPatHandle    pixpat;
  94.     
  95.     initMac();
  96.     
  97.     //pixmap = (PixMapHandle)NewHandle( sizeof( PixMap ) );
  98.     pixmap = NewPixMap();
  99.     pixpat = NewPixPat();
  100.     
  101.     createWindow();
  102.     
  103.     createPixMap( pixmap );
  104.     PixMap2PixPat( pixmap, pixpat );
  105.     PixPat2ppat( pixpat );
  106.     
  107.     doEventLoop( pixpat );
  108. }
  109.  
  110. void initMac()
  111. {
  112.     /*MaxApplZone();
  113.     
  114.     InitGraf( &qd.thePort );
  115.     InitFonts();
  116.     InitWindows();
  117.     InitMenus();
  118.     TEInit();
  119.     InitDialogs( nil );*/
  120.     InitCursor();
  121.     FlushEvents( 0, everyEvent );
  122. }
  123.  
  124. void createWindow()
  125. {
  126.     Rect rect;
  127.     BitMap    bitMap;
  128.     int    top, left;
  129.     
  130.     GetQDGlobalsScreenBits(&bitMap);
  131.     
  132.     left = (((bitMap.bounds.right - bitMap.bounds.left) - WWIDTH) / 2);
  133.     top = (((bitMap.bounds.bottom - bitMap.bounds.top) - WHEIGHT) / 2);
  134.     
  135.     //SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  136.     SetRect( &rect, left, top, left + WWIDTH, top + WHEIGHT );
  137.     
  138.     gWindow = NewCWindow( 0L, &rect, "\pPixMap2PixPat2ppat", true, noGrowDocProc,
  139.                             (WindowPtr)-1L, true, 0L );
  140.     //SetPort( gWindow );
  141.     SetPortWindowPort( gWindow );
  142. }
  143.  
  144. void createPixMap( pixmap )
  145. PixMapHandle    pixmap;
  146. {
  147.     Handle        iclHandle;
  148.     char        depth;
  149.     Rect        rect;
  150.         
  151.     SetRect( &rect, 0, 0, 32, 32 );
  152.     depth = 8;
  153.     
  154.     /* Create a pixmap from the icl8 pixel image. */
  155.     
  156.     iclHandle = GetResource( 'icl8', 129 );
  157.     
  158.     HLock( iclHandle );
  159.     HNoPurge( iclHandle );
  160.         
  161.     (**pixmap).baseAddr = *iclHandle;
  162.     (**pixmap).rowBytes = (((rect.right - rect.left) * depth) / 8) | 0x8000;
  163.     (**pixmap).bounds = rect;
  164.     (**pixmap).pmVersion = 0;
  165.     (**pixmap).packType = 0;
  166.     (**pixmap).packSize = 0;
  167.     (**pixmap).hRes = 0x00480000;
  168.     (**pixmap).vRes = 0x00480000;
  169.     (**pixmap).pixelSize = depth;
  170.     //(**pixmap).planeBytes = 0;
  171.     //(**pixmap).pmReserved = 0;
  172.     (**pixmap).pixelType = 0;
  173.     (**pixmap).cmpCount = 1;
  174.     (**pixmap).cmpSize = depth;
  175.     (**pixmap).pmTable = GetCTable( depth );
  176. }
  177.  
  178. void PixMap2PixPat( pixmap, pixpat )
  179. PixMapHandle    pixmap;
  180. PixPatHandle    pixpat;
  181. {
  182.     Handle      image;
  183.     long        imageSize;
  184.     
  185.     //    Dispose of the old patMap first
  186.     DisposePixMap( (**pixpat).patMap );
  187.     
  188.     (**pixpat).patMap = pixmap;
  189.     
  190.     //    Compute the size of the image using GetPixRowBytes(), not rowBytes!
  191.     imageSize = GetPixRowBytes((**pixpat).patMap) *
  192.                 ((**(**pixpat).patMap).bounds.bottom -
  193.                 (**(**pixpat).patMap).bounds.top);
  194.                 
  195.     PtrToHand( (**pixmap).baseAddr, &image, imageSize );
  196.     (**pixpat).patData = image;
  197. }
  198.  
  199. void PixPat2ppat( pixpat )
  200. PixPatHandle    pixpat;
  201. {
  202.     Handle            ppat;
  203.     PixPatHandle    pixpatCopy;
  204.     long            pixmapSize, pixpatSize, imageSize, ctableSize;
  205.     
  206.     pixpatCopy = NewPixPat();
  207.     
  208.     HLock((Handle)pixpatCopy);
  209.     HLock((Handle)pixpat);
  210.     
  211.     CopyPixPat( pixpat, pixpatCopy );
  212.     
  213.     pixmapSize    = sizeof( PixMap );
  214.     pixpatSize    = sizeof( PixPat );
  215.     imageSize    = GetPixRowBytes((**pixpatCopy).patMap) *
  216.                 ((**(**pixpatCopy).patMap).bounds.bottom -
  217.                 (**(**pixpatCopy).patMap).bounds.top);
  218.                 
  219.     ctableSize    = sizeof( ColorTable ) +
  220.                 (**(**(**pixpatCopy).patMap).pmTable).ctSize *
  221.                 sizeof( ColorSpec );
  222.     
  223.     /********************************************/
  224.     /* Allocate memory for the 'ppat' resource. */
  225.     /********************************************/
  226.     
  227.     ppat = NewHandleClear( pixpatSize + pixmapSize + imageSize + ctableSize );
  228.     HLock((Handle)ppat);
  229.     
  230.     /**********************/
  231.     /* Fill the resource. */
  232.     /**********************/
  233.     
  234.     BlockMove( *(**(**pixpatCopy).patMap).pmTable,
  235.                 *ppat + pixpatSize + pixmapSize + imageSize, ctableSize );
  236.     (**(**pixpatCopy).patMap).pmTable = (CTabHandle)(pixpatSize + pixmapSize + imageSize);
  237.     
  238.     BlockMove( *(**pixpatCopy).patData, *ppat + sizeof( PixPat ) + sizeof( PixMap), imageSize );
  239.     (**pixpatCopy).patData = (Handle)(pixpatSize + pixmapSize);
  240.     
  241.     BlockMove( *(**pixpatCopy).patMap, *ppat + sizeof( PixPat ), pixmapSize );
  242.     (**pixpatCopy).patMap = (PixMapHandle)pixpatSize;
  243.     
  244.     BlockMove( *pixpatCopy, *ppat, pixpatSize );
  245.     
  246.     /************************************************/
  247.     /* Finally, save the 'ppat' resource to a file. */
  248.     /************************************************/
  249.  
  250.     ppat2file( ppat, "\pppat file.rsrc" );
  251.     
  252.     HUnlock((Handle)ppat);
  253.     HUnlock((Handle)pixpatCopy);
  254.     HUnlock((Handle)pixpat);
  255. }
  256.  
  257. void ppat2file( Handle ppat, Str255 fname )
  258. {
  259.     // Old routine deleted, replaced by all carbon routines using FSSpec
  260.     // Only light error checking, more robust apps should have more extensive
  261.     // error checking.
  262.     OSErr    anErr;
  263.     FSSpec    fspec;
  264.     int        refNum;
  265.     
  266.     anErr = FSMakeFSSpec(0, 0, fname, &fspec);
  267.     
  268.     FSpCreateResFile(&fspec, 'RSED', 'rsrc', smSystemScript);
  269.         
  270.     if (ResError() == dupFNErr) {
  271.         anErr = FSpDelete(&fspec);
  272.         FSpCreateResFile(&fspec, 'RSED', 'rsrc', smSystemScript);
  273.     }
  274.     
  275.     if (ResError() != noErr || (anErr != noErr && anErr != fnfErr))
  276.         return;
  277.     
  278.     refNum = FSpOpenResFile(&fspec, fsRdWrPerm);
  279.     
  280.     anErr = ResError();
  281.     
  282.     if (anErr == noErr) {
  283.         AddResource( ppat, 'ppat', UniqueID( 'ppat' ), "\pMy ppat" );
  284.         anErr = ResError();
  285.         CloseResFile(refNum);
  286.         anErr = ResError();
  287.     }
  288.     
  289. }
  290.  
  291. void drawWindow( pixpat )
  292. PixPatHandle    pixpat;
  293. {
  294.     Rect    tempRect1;
  295.     
  296.     //FillCRect( &(*gWindow).portRect, pixpat );
  297.     FillCRect(GetPortBounds(GetWindowPort(gWindow), &tempRect1), pixpat);
  298.     
  299.     TextFont( kFontIDTimes );
  300.     TextSize( 72 );
  301.     TextMode( srcOr );
  302.     
  303.     ForeColor( blackColor );
  304.     MoveTo( 80, 100 );
  305.     DrawString( "\pDone." );
  306.     
  307.     ForeColor( yellowColor );
  308.     MoveTo( 75, 95 );
  309.     DrawString( "\pDone." );
  310. }
  311.  
  312. void doEventLoop( pixpat )
  313. PixPatHandle    pixpat;
  314. {
  315.     EventRecord event;
  316.     WindowPtr   window;
  317.     short       clickArea;
  318.     Rect        screenRect;
  319.  
  320.     for (;;)
  321.     {
  322.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  323.         {
  324.             if (event.what == mouseDown)
  325.             {
  326.                 clickArea = FindWindow( event.where, &window );
  327.                 
  328.                 if (clickArea == inDrag)
  329.                 {
  330.                     //screenRect = (**GetGrayRgn()).rgnBBox;
  331.                     GetRegionBounds(GetGrayRgn(), &screenRect);
  332.                     DragWindow( window, event.where, &screenRect );
  333.                 }
  334.                 else if (clickArea == inContent)
  335.                 {
  336.                     if (window != FrontWindow())
  337.                         SelectWindow( window );
  338.                 }
  339.                 else if (clickArea == inGoAway)
  340.                     if (TrackGoAway( window, event.where ))
  341.                         return;
  342.             }
  343.             else if (event.what == updateEvt)
  344.             {
  345.                 window = (WindowPtr)event.message;    
  346.                 //SetPort( window );
  347.                 SetPortWindowPort( window );
  348.                 
  349.                 BeginUpdate( window );
  350.                 drawWindow( pixpat );
  351.                 EndUpdate( window );
  352.             }
  353.         }
  354.     }
  355. }